package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * GET    /users
     * 请求参数：pagesize   page
     * 返回数据： PageRe
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = manageService.users(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询用户详细信息
     * GET        /users/{userId}
     * 返回值：UserInfo
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity users(@PathVariable("userId") Long userId) {
        UserInfo vo = manageService.findByUserId(userId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询用户的视频列表
     * GET    /videos
     * 请求参数：page pagesize   uid
     * 返回数据：PageResult
     */
    @GetMapping("/videos")
    public ResponseEntity findVideos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 Long uid) {
        PageResult result = manageService.findVideos(uid, page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询动态列表
     * GET    /messages
     */
    @GetMapping("/messages")
    public ResponseEntity findMovements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        Long uid, String state) {
        Integer status = null;
        if (!StringUtils.isEmpty(state) && !"''".equals(state)) {
            status = Integer.valueOf(state);
        }
        PageResult vo = manageService.findMovements(status, uid, page, pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询动态详情
     * GET      /messages/{movementId}
     * 路径参数： id
     * 返回值：movementVo
     */
    @GetMapping("/messages/{movementId}")
    public ResponseEntity messages(@PathVariable("movementId") String movementId) {
        MovementsVo vo = manageService.findMovement(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询评论列表
     * GET    /messages/comments
     * 请求参数：Query：  page   pagesize  messageID
     */
    @GetMapping("/messages/comments")
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String messageID) {
        PageResult vo = manageService.findComments(page, pagesize, messageID);
        return ResponseEntity.ok(vo);
    }

    /**
     * 用户冻结操作
     * POST  /users/freeze
     * 请求参数：Body:
     *      userId：冻结用户id
     *      freezingTime：冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
     *      freezingRange：冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
     *      reasonsForFreezing：冻结原因
     *      frozenRemarks：冻结备注
     * 返回：
     *      String：结果
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map map){
        String string = map.get("userId").toString();
        Long userId = Long.valueOf(string);
        String string1 = map.get("freezingTime").toString();
        Integer freezingTime = Integer.valueOf(string1);
        String string2 = map.get("freezingRange").toString();
        Integer freezingRange = Integer.valueOf(string2);
        String reasonsForFreezing = map.get("reasonsForFreezing").toString();
        String frozenRemarks = map.get("frozenRemarks").toString();
        String message = manageService.freeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
        return ResponseEntity.ok(message);
    }

    /**
     * 用户解冻
     * POST    /users/unfreeze
     * 请求参数：Body:
     *      userId：用户id
     *      reasonsForThawing；解冻原因
     * 返回：
     *      message：操作结果
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map){
        String string = map.get("userId").toString();
        Long userId = Long.valueOf(string);
        String reasonsForThawing = map.get("reasonsForThawing").toString();
        String message = manageService.unfreeze(userId,reasonsForThawing);
        return ResponseEntity.ok(message);
    }
}
